package com.zxy.service;

import cn.hutool.core.date.DateUtil;
import com.zxy.dao.OrderItemDao;
import com.zxy.dao.OrderMasterDao;
import com.zxy.feignclient.StockClient;
import com.zxy.model.Ordering;
import com.zxy.model.OrderingOk;
import com.zxy.model.OrderingProduct;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderService {
    @Autowired
    private OrderMasterDao masterDao;
    @Autowired
    private OrderItemDao itemDao;
    @Autowired
    private StockClient stockClient;
//    @Transactional
    @GlobalTransactional(name = "create_order")
    public OrderingOk order(Ordering ordering){

        String yyyymmddhHmmssSSS = DateUtil.format(DateUtil.date(), "YYYYMMDDHHmmssSSS");
        Long id = Long.valueOf(yyyymmddhHmmssSSS);
        ordering.setId(Long.valueOf(yyyymmddhHmmssSSS));

        masterDao.Insert(ordering);
        List<OrderingProduct> products = ordering.getProducts();
        for (OrderingProduct product : products) {
            product.setOrderId(id);
            stockClient.ordering(product.getProductId(), product.getQty());
            itemDao.Insert(product);
        }
        OrderingOk orderingOk = new OrderingOk();
        orderingOk.setOrderId(id);
        return orderingOk;
    }











}
